package chordcows;

import java.net.MalformedURLException;
import java.util.Scanner;
import java.util.StringTokenizer;

import de.uniba.wiai.lspi.chord.data.URL;



public class Main {
	
	public FileServer server;
	public String fileDir;
	
	public static void main(String[] args){
		
		Main main = new Main(args[0]);
		
		main.waitForUserInput();
	}
	
	public Main(String fileDir){
		this.fileDir = fileDir;
		System.out.println("File directory set to: "+fileDir);
	}
	
	public void create(){
		URL bootstrapURL = null;
		try {
			bootstrapURL = new URL ( URL.KNOWN_PROTOCOLS.get(URL.SOCKET_PROTOCOL ) + "://169.254.209.102:8080/" );
		} catch ( MalformedURLException e ) {
			throw new RuntimeException ( e );
		}
		
		
		server = new FileServer(fileDir);
		server.createNetwork();
		FileServer myServer2 = new FileServer(fileDir,8181,8585,2223);
		myServer2.joinNetwork(bootstrapURL);
		server.start();
		
		myServer2.leave();
	}
	
	public void waitForUserInput(){
		Scanner sc = new Scanner(System.in);
		StringTokenizer tokenizer;
		int tokenCount=0;
		String token;
		while(true){
			tokenizer = new StringTokenizer(sc.nextLine());
			tokenCount = tokenizer.countTokens();
			if(tokenizer.hasMoreTokens()){
				token = tokenizer.nextToken();
				
				if(token.equals("create")){
					create();	
				}
				else if(token.equals("join")){
					if(tokenCount==4){
						join(tokenizer.nextToken(),tokenizer.nextToken(),tokenizer.nextToken());
					}
					else if(tokenCount==6){
						join(tokenizer.nextToken(),tokenizer.nextToken(),tokenizer.nextToken(),tokenizer.nextToken(),tokenizer.nextToken());
					}
					else{
						System.out.println("Invalid Option");
						System.out.println("join <myPort> <RemoteIP> <RemotePort>");
						continue;
					}
				}
				else if(token.equals("add")){
					if(!tokenizer.hasMoreTokens()){
						System.out.println("Invalid Option");
						continue;
					}
					server.addToMaster(tokenizer.nextToken());
				}
				else if(token.equals("remove")){
					//TODO
				}
				else if(token.equals("sync")){
					server.syncServer();
				}
				else if(token.equals("display")){
					server.displayMaster();
				}
				else if(token.equals("exit")){
					server.leave();
					break;
				}
				else
					System.out.println("Invalid Option");
			}
			
		}
	}
		
	public void join(String myPort, String IP, String port ){
		URL bootstrapURL = null;
		try {
			bootstrapURL = new URL ( URL.KNOWN_PROTOCOLS.get(URL.SOCKET_PROTOCOL ) + "://"+IP+":"+port+"/" );
		} catch ( MalformedURLException e ) {
			throw new RuntimeException ( e );
		}
		
		server = new FileServer(fileDir,Integer.parseInt(myPort));
		server.joinNetwork(bootstrapURL);
		server.start();
	}
	
	public void join(String myPort, String IP, String port, String RMIport, String transferPort){
		URL bootstrapURL = null;
		try {
			bootstrapURL = new URL ( URL.KNOWN_PROTOCOLS.get(URL.SOCKET_PROTOCOL ) + "://"+IP+":"+port+"/" );
		} catch ( MalformedURLException e ) {
			throw new RuntimeException ( e );
		}
		
		server = new FileServer(fileDir,Integer.parseInt(myPort),Integer.parseInt(RMIport),Integer.parseInt(transferPort));
		server.joinNetwork(bootstrapURL);
		server.start();
	}
}
	
	
	
	
	

